package com.neusoft.hifly.attachment.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

/**
 * Title: 核心类库
 * <p>
 * Description: springboot引入外部xml配置文件
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Configuration
public class UploadConfig {

	// 附件上传地址，注意要以/结尾
	@Value("${upload.path:/data/webapps/}")
	private String uploadPath;

	// 附件资源服务器地址
	@Value("${upload.domain:http://files.ctcloud.com.cn}")
	private String fileDomain;

	// 设置一个大小，multipart请求小于这个大小时会存到内存中，大于这个内存会存到硬盘中
	@Value("${upload.multipart.max-in-memory-size:40960}")
	private int maxInMemorySize;

	// 设置multipart请求所允许的最大大小
	@Value("${upload.multipart.max-upload-size:52428800}")
	private long maxUploadSize;

	// 显示声明CommonsMultipartResolver为mutipartResolver
	@Bean(name = "multipartResolver")
	public MultipartResolver multipartResolver() {
		final CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		// 默认编码
		resolver.setDefaultEncoding("UTF-8");
		// resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
		resolver.setResolveLazily(true);
		// 设置一个大小，multipart请求小于这个大小时会存到内存中，大于这个内存会存到硬盘中
		resolver.setMaxInMemorySize(this.getMaxInMemorySize());
		// 设置multipart请求所允许的最大大小，默认不限制 50M 5*1024*1024
		resolver.setMaxUploadSize(this.getMaxUploadSize());
		return resolver;
	}

	public String getUploadPath() {
		return uploadPath;
	}

	public void setUploadPath(final String uploadPath) {
		this.uploadPath = uploadPath;
	}

	public String getFileDomain() {
		return fileDomain;
	}

	public void setFileDomain(final String fileDomain) {
		this.fileDomain = fileDomain;
	}

	public int getMaxInMemorySize() {
		return maxInMemorySize;
	}

	public void setMaxInMemorySize(final int maxInMemorySize) {
		this.maxInMemorySize = maxInMemorySize;
	}

	public long getMaxUploadSize() {
		return maxUploadSize;
	}

	public void setMaxUploadSize(final long maxUploadSize) {
		this.maxUploadSize = maxUploadSize;
	}
}
